package Controller;

import Service.DealService;
import Service.OpenService;
import Service.StockService;
import Utils.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Map;

@Controller
public class DealController
{
    @Autowired
    private DealService dealService;
    @Autowired
    private StockService stockService;
    @ResponseBody
    @RequestMapping(value = "/API/Deal/AddBuy",method = RequestMethod.POST)
    public JsonResult AddBuy(@RequestBody String params, HttpSession session)
    {
        JsonResult jsonResult = new JsonResult();

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String,String> params_map = objectMapper.readValue(params,Map.class);
            String UserId = null;
            UserId = (String) session.getAttribute("UserId");
            String StockCode = null;
            StockCode = params_map.get("StockCode");
            String BuyCount = null;
            BuyCount  = params_map.get("BuyCount");
            float MaxPrice;
            MaxPrice = Float.parseFloat(params_map.get("MaxPrice"));

            StockInfo stock = stockService.getStockInfoByCode(StockCode);
            if(stock == null)
            {
                jsonResult.setErrorCode("所购买的股票代码不存在，请修改");
                return jsonResult;
            }

            Buy buy = new Buy();
            buy.setUserID(UserId);
            buy.setStockCode(StockCode);
            buy.setBuyCount(BuyCount);
            buy.setMaxPrice(MaxPrice);
            boolean status = dealService.Add_Buy(buy);

            if(status)
            {
                jsonResult.setErrorCode("提交买入委托成功");
            }
            else
            {
                jsonResult.setErrorCode("发生异常，请联系管理员处理");
            }
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生错误，请联系管理员");
        }
        finally {
            return jsonResult;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/API/Deal/AddSell",method = RequestMethod.POST)
    public JsonResult AddSell(@RequestBody String params, HttpSession session)
    {
        JsonResult jsonResult = new JsonResult();

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String,String> params_map = objectMapper.readValue(params,Map.class);
            String UserId = null;
            UserId = (String) session.getAttribute("UserId");
            String StockCode = null;
            StockCode = params_map.get("StockCode");
            String SellCount = null;
            SellCount  = params_map.get("SellCount");
            float LowestPrice;
            LowestPrice = Float.parseFloat(params_map.get("LowestPrice"));
            StockInfo stock = stockService.getStockInfoByCode(StockCode);
            if(stock == null)
            {
                jsonResult.setErrorCode("所出售的股票代码不存在，请修改");
                return jsonResult;
            }
            Sell sell = new Sell();
            sell.setUserID(UserId);
            sell.setStockCode(StockCode);
            sell.setSellCount(SellCount);
            sell.setLowestPrice(LowestPrice);
            boolean status = dealService.Add_Sell(sell);
            if(status)
            {
                jsonResult.setErrorCode("提交卖出委托成功");
            }
            else
            {
                jsonResult.setErrorCode("发生异常，请联系管理员处理");
            }
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生错误，请联系管理员");
        }
        finally {
            return jsonResult;
        }
    }
    @ResponseBody
        @RequestMapping(value = "/API/Deal/GetAllDeal",method = RequestMethod.GET)
    public JsonResult GetAllDeal(HttpSession session)
    {
        JsonResult jsonResult = new JsonResult();
        ArrayList<Deal> dealArrayList = null;
        try {
            String UserId = null;
            UserId = (String) session.getAttribute("UserId");
            dealArrayList = dealService.GetAllDealByUserId(UserId);
            jsonResult.setData(dealArrayList);
            jsonResult.setErrorCode("调用成功");

        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生错误，请联系管理员");
        }
        finally {
            return jsonResult;
        }
    }
}
